import java.io.IOException;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;

import devboard.dbutil.MongoUtil;

@WebServlet("/AJAXHandler")
public class AJAXBoardHandler extends HttpServlet {
	
	enum Reason {
		UPDATE_TASK{
			@Override
			public String processRequest(HttpServletRequest request) throws IOException{
				String id=request.getParameter("id");
				DBCollection tasks=MongoUtil.getCollection("Tasks");
				DBObject task=tasks.findOne(new BasicDBObject("_id", id));
				if(task!=null){
					task.put("name", request.getParameter("name"));
					task.put("link", request.getParameter("link"));
					task.put("userId", request.getParameter("account_id"));
					task.put("projectId", request.getParameter("project_id"));
					tasks.update(new BasicDBObject("_id", id),task);
					MongoUtil.sendMessage("The user <b>"+(String)request.getSession().getAttribute("firstName")+
							(String)request.getSession().getAttribute("lastName")
							+"</b> has updated task.","devboard", "devboard");
					return "success";
				}else{
					return "error";
				}
			}
		},
		UPDATE_TASK_STATUS{
			@Override
			public String processRequest(HttpServletRequest request) throws IOException{
				String id=request.getParameter("id");
				DBCollection tasks=MongoUtil.getCollection("Tasks");
				DBObject task=tasks.findOne(new BasicDBObject("_id", id));
				if(task!=null){
					if(((String)request.getSession().getAttribute("_id")).equals(task.get("userId"))){
						if(task.get(request.getParameter("name")).equals("issue")){
							return (String)task.get("issue");
						}else{
							String status=task.get(request.getParameter("name")).equals("no")?"yes":"no";
							task.put(request.getParameter("name"), status);
							tasks.update(new BasicDBObject("_id", id),task);
							MongoUtil.sendMessage("The user <b>"+(String)request.getSession().getAttribute("firstName")+
									(String)request.getSession().getAttribute("lastName")
									+"</b> has updated task's status.","devboard", "devboard");
							return status;
						}
					}else{
						if(task.get(request.getParameter("name")).equals("issue")){
							return (String)task.get("issue");
						}
						else return "no_rights";	
					}
				}else{
					return "internal server error";
				}
			}
		},
		DELETE_TASK{
			@Override
			public String processRequest(HttpServletRequest request) throws IOException{
				DBCollection tasks=MongoUtil.getCollection("Tasks");
				DBObject task=tasks.findAndRemove(new BasicDBObject("_id", request.getParameter("id")));
				if(task==null){
					return "error";
				}
				MongoUtil.sendMessage("The user <b>"+(String)request.getSession().getAttribute("firstName")+
						(String)request.getSession().getAttribute("lastName")
						+"</b> has deleted the task.","devboard", "devboard");
				return "deleted";
			}
		},
		CREATE_ISSUE{
			@Override
			public String processRequest(HttpServletRequest request) throws IOException{
				String id=request.getParameter("id");
				DBCollection tasks=MongoUtil.getCollection("Tasks");
				DBObject task=tasks.findOne(new BasicDBObject("_id", id));
				String issue=request.getParameter("issueContent");
				if((issue!=null)&&(task!=null)){
					task.put("issue",issue);
					Set<String> keys=task.keySet();
					for(String key:keys){
						if(task.get(key).equals("no")){
							task.put(key, "issue");
							break;
						}
				}
					tasks.update(new BasicDBObject("_id", id),task);
				}
				MongoUtil.sendMessage("The user <b>"+(String)request.getSession().getAttribute("firstName")+
						(String)request.getSession().getAttribute("lastName")
						+"</b> has created issues.","devboard", "devboard");
				return "";
			}
		},
		RESOLVE_ISSUE{
			@Override
			public String processRequest(HttpServletRequest request) throws IOException{
				String id=request.getParameter("id");
				DBCollection tasks=MongoUtil.getCollection("Tasks");
				DBObject task=tasks.findOne(new BasicDBObject("_id", id));
				if(task!=null){
					String status="no";
					task.put(request.getParameter("name"), status);
					tasks.update(new BasicDBObject("_id", id),task);
					MongoUtil.sendMessage("The user <b>"+(String)request.getSession().getAttribute("firstName")+
							(String)request.getSession().getAttribute("lastName")
							+"</b> has resolved to issue.","devboard", "devboard");
					return status;
				}else{
					return "error";
				}
			}
		},
		ADD_TASK{
			@Override
			public String processRequest(HttpServletRequest request) throws IOException{
				DBCollection tasks=MongoUtil.getCollection("Tasks");
				BasicDBObject task=new BasicDBObject();
				task.put("_id",""+(System.currentTimeMillis()));
				task.put("project", request.getParameter("team"));
				task.put("name", request.getParameter("name"));
				task.put("link", request.getParameter("link"));
				task.put("userId", "nobody");
				task.put("reproduced", "no");
				task.put("inProgress", "no");
				task.put("delivered", "no");
				task.put("successfully", "no");
				task.put("timeCreation", System.currentTimeMillis());
				task.put("projectId", request.getParameter("projectId"));
				tasks.insert(task);
				MongoUtil.sendMessage("User <b>"+(String)request.getSession().getAttribute("firstName")+
						(String)request.getSession().getAttribute("lastName")
						+"</b> added a new task.","devboard", "devboard");
				return "";
			}
		};
		
		public abstract String processRequest(HttpServletRequest request) throws IOException;
		
	}
	
	private static final long serialVersionUID = 1L;

    public AJAXBoardHandler() {
        super();
    }
    
    
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Reason reason=Reason.valueOf(request.getParameter("reason"));
		response.getWriter().print(reason.processRequest(request));
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

}
